package com.fulihui.systemcore.repository.impl;

import com.fulihui.systemcore.dal.dataobj.SysAreasExample;
import com.fulihui.systemcore.dal.mapper.SysAreasMapper;
import com.fulihui.systemcore.dto.AreasDTO;
import com.fulihui.systemcore.repository.AreasRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

import static com.fulihui.systemcore.biz.conv.AreasConv.toDTO;
import static com.fulihui.systemcore.biz.conv.AreasConv.toList;

/**
 * @author Willard.Hu on 2016/11/29 0029.
 */
@Repository
public class AreasRepositoryImpl implements AreasRepository {
    @Autowired
    SysAreasMapper areasMapper;

    @Override
    public AreasDTO queryByCode(String code) {
        return toDTO(areasMapper.selectByPrimaryKey(code));
    }

    @Override
    public List<AreasDTO> queryProvionceList() {
        SysAreasExample example = new SysAreasExample();
        example.createCriteria().andLevelEqualTo(1);
        example.setOrderByClause("sort");
        return toList(areasMapper.selectByExample(example));
    }

    @Override
    public List<AreasDTO> queryCityList(String provCode) {
        SysAreasExample example = new SysAreasExample();
        example.createCriteria()
                .andParentCodeEqualTo(provCode)
                .andLevelEqualTo(2);
        example.setOrderByClause("sort");
        return toList(areasMapper.selectByExample(example));
    }

    @Override
    public List<AreasDTO> queryDistrictList(String cityCode) {
        SysAreasExample example = new SysAreasExample();
        example.createCriteria()
                .andParentCodeEqualTo(cityCode)
                .andLevelEqualTo(3);
        example.setOrderByClause("sort");
        return toList(areasMapper.selectByExample(example));
    }

    @Override
    public List<AreasDTO> querySubdistrictList(String distCode) {
        SysAreasExample example = new SysAreasExample();
        example.createCriteria()
                .andParentCodeEqualTo(distCode)
                .andLevelEqualTo(4);
        example.setOrderByClause("sort");
        return toList(areasMapper.selectByExample(example));
    }

}
